﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using ORSLibrary.Service;

namespace ORSLibrary.Security
{
    public class UserGroup
    {
        #region Public Properties

        public int ID { get; set; }
        public string Name { get; set; }
        public string EName { get; set; }

        public DataTable Permissions { get; set; }

        #endregion

        public UserGroup()
        {
            SqlService sql = new SqlService();
            sql.AddParameter("@UserGroupID", SqlDbType.Int, ID);
            Permissions = sql.ExecuteSPDataSet("SecurityPermissionSelectAll", "Permissions").Tables["Permissions"];
        }

        public UserGroup(int ID)
        {
            SqlService sql = new SqlService();
            sql.AddParameter("@ID", SqlDbType.Int, ID);
            SqlDataReader reader = sql.ExecuteSPReader("SecurityUserGroupSelect");

            if (reader.Read())
            {
                this.LoadFromReader(reader);

                reader.Close();

                SqlService sql2 = new SqlService();
                sql2.AddParameter("@UserGroupID", SqlDbType.Int, ID);
                Permissions = sql2.ExecuteSPDataSet("SecurityPermissionSelectAll", "Permissions").Tables["Permissions"];
            }
            else
            {
                if (!reader.IsClosed) reader.Close();
                throw new ApplicationException("Security UserGroup does not exist.");
            }
        }

        public UserGroup(SqlDataReader reader)
        {
            this.LoadFromReader(reader);
        }

        protected void LoadFromReader(SqlDataReader reader)
        {
            if (reader != null && !reader.IsClosed)
            {
                ID = (int)reader["ID"];
                Name = reader["Name"] == DBNull.Value ? string.Empty : reader["Name"].ToString();
                EName = reader["EName"] == DBNull.Value ? string.Empty : reader["EName"].ToString();
            }
        }

        public void Delete()
        {
            UserGroup.Delete(ID);
        }

        public void Update()
        {
            SqlService sql = new SqlService();
            sql.AddParameter("@ID", SqlDbType.Int, ID);
            sql.AddParameter("@Name", SqlDbType.NVarChar, Name);
            sql.AddParameter("@EName", SqlDbType.NVarChar, EName);

            sql.ExecuteSP("SecurityUserGroupUpdate");

            SavePermissions();
        }

        public void Create()
        {
            SqlService sql = new SqlService();

            sql.AddParameter("@ID", SqlDbType.Int, ParameterDirection.Output);
            sql.AddParameter("@Name", SqlDbType.NVarChar, Name);
            sql.AddParameter("@EName", SqlDbType.NVarChar, EName);

            sql.ExecuteSP("SecurityUserGroupInsert");

            this.ID = (int)sql.Parameters["@ID"].Value;

            SavePermissions();
        }

        private void SavePermissions()
        {
            using (SqlConnection MyConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString))
            {
                SqlDataAdapter da = new SqlDataAdapter();

                //Insert Command
                SqlCommand cmdInsert = new SqlCommand();
                cmdInsert.CommandType = CommandType.Text;
                cmdInsert.CommandText = "INSERT INTO SecurityPermission (UserGroupID, MenuID) VALUES (@UserGroupID, @MenuID)";
                cmdInsert.Connection = MyConnection;
                cmdInsert.Parameters.Add(new SqlParameter("@UserGroupID", ID));
                cmdInsert.Parameters.Add("@MenuID", SqlDbType.NVarChar, 50, "MenuID");

                //Delete Command
                SqlCommand cmdDelete = new SqlCommand();
                cmdDelete.CommandType = CommandType.Text;
                cmdDelete.CommandText = "DELETE FROM SecurityPermission WHERE (ID = @ID)";
                cmdDelete.Connection = MyConnection;
                cmdDelete.Parameters.Add("@ID", SqlDbType.Int, 4, "ID");
                da.DeleteCommand = cmdDelete;
                da.InsertCommand = cmdInsert;

                da.Update(Permissions);
            }
        }

        public static UserGroup NewEntity(int ID)
        {
            UserGroup newEntity = new UserGroup();
            newEntity.ID = ID;

            return newEntity;
        }

        public static UserGroup GetEntity(int ID)
        {
            return new UserGroup(ID);
        }

        public static void Delete(int ID)
        {
            SqlService sql = new SqlService();
            sql.AddParameter("@ID", SqlDbType.Int, ID);

            sql.ExecuteSP("SecurityUserGroupDelete");
        }

        public static UserGroupList GetList(bool IsDropDown)
        {
            UserGroupList list = new UserGroupList();

            SqlService sql = new SqlService();
            SqlDataReader reader = sql.ExecuteSPReader("SecurityUserGroupSelectAll");

            if (IsDropDown)
                list.Add(new UserGroup() { ID = 0, Name = "---", EName = "---" });

            while (reader.Read())
            {
                list.Add(new UserGroup(reader));
            }

            reader.Close();

            return list;
        }
    }
    public class UserGroupList : List<UserGroup>
    {
    }
}
